<?php
/**
 * 快递公司
 *
 *
 *
*/

defined('IN_NWIDC') or exit('Access Invalid!');
class expressControl extends SystemControl{
	public function __construct(){
		parent::__construct();
		Language::read('express');
	}

	public function indexOp(){
		$lang	= Language::getLangContent();
		$model = Model('express');
		if (preg_match('/^[A-Z]$/',$_GET['letter'])){
			$model->where(array('e_letter'=>$_GET['letter']));
		}
		$list = $model->page(10)->order('e_order,e_state desc,id')->select();
		Tpl::output('page',$model->showpage());
		Tpl::output('list',$list);
		Tpl::showpage('express.index');
	}

	/**
	 * ajax操作
	 */
	public function ajaxOp(){
		switch ($_GET['branch']){
			case 'state':
				$model_brand = Model('express');
				$update_array = array();
				$update_array['id'] = intval($_GET['id']);
				$update_array[$_GET['column']] = trim($_GET['value']);
				$model_brand->update($update_array);
				dkcache('express');
				$this->log(L('nc_edit,express_name,express_state').'[ID:'.intval($_GET['id']).']',1);
				echo 'true';exit;
				break;
			case 'order':
				$_GET['value'] = $_GET['value'] == 0? 2:1;
				$model_brand = Model('express');
				$update_array = array();
				$update_array['id'] = intval($_GET['id']);
				$update_array[$_GET['column']] = trim($_GET['value']);
				$model_brand->update($update_array);
				dkcache('express');
				$this->log(L('nc_edit,express_name,express_state').'[ID:'.intval($_GET['id']).']',1);
				echo 'true';exit;
				break;
    		case 'e_zt_state':
    		    $model_brand = Model('express');
    		    $update_array = array();
    		    $update_array['id'] = intval($_GET['id']);
    		    $update_array[$_GET['column']] = trim($_GET['value']);
    		    $model_brand->update($update_array);
				dkcache('express');
    		    $this->log(L('nc_edit,express_name,express_state').'[ID:'.intval($_GET['id']).']',1);
    		    echo 'true';exit;
    		    break;
		}
		dkcache('express');
	}

	public function editOp() {
		if (count($_POST)) {
			$data = array(
				'e_name' => $_POST['e_name'],
				'e_code' => $_POST['e_code'],
				'e_url' => $_POST['e_url'],
			);

			if (empty($_POST['id'])) {
				Model()->table('express')->insert(($data));
			}
			else{
				Model()->table('express')->where(array('id' => $_POST['id']))->update(($data));
			}
			dkcache('express');
			header("location:/admin/index.php?act=express");
			exit;
		}

		Tpl::showpage('express.edit');
	}

	public function addOp() {
		Tpl::showpage('express.edit');
	}

	public function deleteOp() {
		Model('express')->where(array('id' => $_GET['id']))->delete();
		dkcache('express');
		header("location:/admin/index.php?act=express");
	}

}
